package com.linln.admin.order.service.impl;

import com.linln.admin.core.enums.StatusEnum;
import com.linln.admin.order.domain.As;
import com.linln.admin.order.repository.AsRepository;
import com.linln.admin.order.service.AsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author 小懒虫
 * @date 2019/04/11
 */
@Service
public class AsServiceImpl implements AsService {

    @Autowired
    private AsRepository asRepository;

    /**
     * 根据asID查询as数据
     * @param id asID
     */
    @Override
    @Transactional
    public As getId(Long id) {
        Byte[] status = {StatusEnum.OK.getCode(), StatusEnum.FREEZED.getCode()};
        return asRepository.findByIdAndStatusIn(id, status);
    }

    /**
     * 获取分页列表数据
     * @param example 查询实例
     * @param pageIndex 页码
     * @param pageSize 获取列表长度
     * @return 返回分页数据
     */
    @Override
    public Page<As> getPageList(Example<As> example, Integer pageIndex, Integer pageSize) {
        // 创建分页对象
        Sort sort = new Sort(Sort.Direction.DESC, "createDate");
        PageRequest page = PageRequest.of(pageIndex-1, pageSize, sort);
        Page<As> list = asRepository.findAll(example, page);
        return list;
    }

    /**
     * 保存as
     * @param as as实体类
     */
    @Override
    public As save(As as){
        return asRepository.save(as);
    }

    /**
     * 状态(启用，冻结，删除)/批量状态处理
     */
    @Override
    @Transactional
    public Integer updateStatus(StatusEnum statusEnum, List<Long> idList){
        return asRepository.updateStatus(statusEnum.getCode(),idList);
    }
}

